<?php
class CFilter
{
	static function get_condition(&$filter)
	{
		$operator = isset($filter['operator'])?$filter['operator']:'EEqual';
		if(!isset($filter['type']))
		{
			$filter['type'] = 'text';
		}
		if(isset($filter['id']))
		{
			$filter['request'] = request('filter-'.$filter['id']);
		}
		if(!isset($filter['input_type']))
		{
			$filter['input_type'] = $filter['type'];
		}
		switch($filter['input_type'])
		{
			case 'date':
				$filter['init_code'] = '
	<script type="text/javascript">
	SYS.load(
		\'3rdparty/jQuery/jquery.ui/themes/\'+SYS.UITheme+\'/ui.all.css\',
		\'3rdparty/jQuery/jquery.ui/ui/ui.core.js\', function(){
		SYS.load(\'3rdparty/jQuery/jquery.ui/ui/ui.datepicker.js\', function(){
			$(\'#filter-'.$filter['id'].module()->id.'\').datepicker({
				dateFormat: \'dd/mm/yy\'
				'.((isset($filter['options']) and trim($filter['options']))?','.$filter['options']:'').'
				'.(isset($filter['options'])?$filter['options']:'').'
			});
		})
	});
	</script>';
				break;
			case 'datetime':
				$filter['init_code'] = '
	<script type="text/javascript">
	SYS.load(
		\'3rdparty/jQuery/jquery.ui/ui.datetimepicker/dark.datetimepicker.css\',
		\'3rdparty/jQuery/jquery.ui/ui.datetimepicker/ui.datetimepicker.js\', function(){
		SYS.load(\'3rdparty/jQuery/jquery.ui/ui.datetimepicker/ui.datetimepicker-vi.js\', function(){
			$(\'#filter-'.$filter['id'].module()->id.'\').datetimepicker({
				dateFormat: \'dd/mm/yy\'
				'.((isset($filter['options']) and trim($filter['options']))?','.$filter['options']:'').'
			});				
		})
	});
	</script>';
				break;
		}
		switch($filter['type'])
		{
			case 'recent-date':
				switch(request('filter-'.$filter['id']))
				{
					case 'today':
						return EAnd(EEqual('year',date('Y')),EEqual('month',date('m')),EEqual('day',date('d')));
						break;
					case '7day':
						$year = date('Y', time()-7*24*3600);
						$month = date('m', time()-7*24*3600);
						$day = date('d', time()-7*24*3600);
						return EOr(EGT('year',$year),
							EAnd(EEqual('year',$year),EOr(
								EGT('month',$month),
								EAnd(EEqual('month', $month),
									EGTE('day', $day)
								)
							))
						);
						break;
					case '30day':
						$year = date('Y', time()-30*24*3600);
						$month = date('m', time()-30*24*3600);
						$day = date('d', time()-30*24*3600);
						return EOr(EGT('year',$year),
							EAnd(EEqual('year',$year),EOr(
								EGT('month',$month),
								EAnd(EEqual('month', $month),
									EGTE('day', $day)
								)
							))
						);
						break;
				}
				break;
			case 'split-date':
				if(request('filter-'.$filter['id']))
				{
					$time = CDateTime(request('filter-'.$filter['id']))->time;
					$year = date('Y', $time);
					$month = date('m', $time);
					$day = date('d', $time);
				}
				else
				if($year = request('filter-'.$filter['id'].'-year'))
				{
					$month = request('filter-'.$filter['id'].'-month');
					$day = request('filter-'.$filter['id'].'-day');
				}
				else
				{
					break;
				}
				if($year)
				{
					$oe = $operator.'E';
					if($month)
					{
						if($day)
						{
							$filter['request'] = $day.'/'.$month.'/'.$year;
							$condition = EOr(
								$operator('month', EInt($month)), 
								EAnd(
									EEqual('month', EInt($month)),
									$oe('day', EInt($day))
								)
							);
						}
						else
						{
							$filter['request'] = 'th�ng '.$month.' nam '.$year;
							$condition = $oe('month', EInt($month));
						}
						$condition = EOr(
							$operator('year', EInt($year)), 
							EAnd(
								EEqual('year', EInt($year)),
								$condition
							)
						);
					}
					else
					{
						$filter['request'] = false;//'Nam '.$year;
						$condition = $oe('year', EInt($year));
					}
					return $condition;
				}
				break;
			case 'date':
			case 'datetime':
				if($value = request('filter-'.$filter['id']) or $value = setting($filter['id']))
				{
					$filter['request'] = $value;
					return $operator(isset($filter['field'])?$filter['field']:$filter['id'], EInt(CDateTime($value)->time));
				}
				break;
			case 'text':
			case 'reference':
				if($text = request('filter-'.$filter['id']))
				{
					$operator = isset($filter['operator'])?$filter['operator']:'ELike';
					return $operator(isset($filter['field'])?$filter['field']:$filter['id'], EString($text));
				}
				break;
			case 'number':
				if($number = request('filter-'.$filter['id']))
				{
					return $operator(isset($filter['field'])?$filter['field']:$filter['id'], EInt($number));
				}
				break;
			case 'select':
				if(isset($filter['data']))
				{
					if(isset($filter['data']['table']))
					{
						$table = CTable($filter['data']['table']);
						if(isset($filter['data']['store']))
						{
							eval('$store='.$filter['data']['store'].';');
							$table->change_store($store);
						}
						$options = $table->select_all(isset($filter['data']['condition'])?$filter['data']['condition']:false, isset($filter['data']['order-by'])?array('order_by'=>$filter['data']['order-by']):array());
					}
					else
					if(isset($filter['data']['code']))
					{
						eval($filter['data']['code'].';');
					}
					else
					{
						$options = $filter['options'];
					}
					if(isset($filter['data']['prepare_data']))
					{
						eval($filter['data']['prepare_data'].';');
					}
					if(isset($filter['data']['label-field']))
					{
						$label_field = $filter['data']['label-field'];
						foreach($options as &$option)
						{
							$option['label'] = $option[$label_field];
						}
					}
					if(isset($filter['data']['id-field']))
					{
						$id_field = $filter['data']['id-field'];
						foreach($options as &$option)
						{
							$option['id'] = $option[$id_field];
						}
					}
					$filter['options'] = $options;
				}
				if($value = request('filter-'.$filter['id']))
				{
					return $operator(isset($filter['field'])?$filter['field']:$filter['id'], EString($value));
				}
				break;
		}
	}
}
function CFilter($filter)
{
	return new CFilter($filter);
}
?>